/**
 * MIT License
 * 
 * Copyright (c) 2017 CaiDongyu
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.tunnel.user.core;

import java.net.InetAddress;

import org.axe.util.MD5Util;

import com.tunnel.client.config.Config;
import com.tunnel.client.tunnel.TunnelChannelHandlerContextManager;
import com.tunnel.client.tunnel.TunnelClient;
import com.tunnel.common.constant.Constant;
import com.tunnel.common.tunnel.TunnelDataQueueManager;
import com.tunnel.common.util.LogUtil;
import com.tunnel.common.util.StringUtil;
import com.tunnel.user.common.SysConfig;

import io.netty.channel.ChannelHandlerContext;

public class TunnelClientHandler extends com.tunnel.client.tunnel.TunnelClientHandler {
	
	public TunnelClientHandler(TunnelClient tunnelClient) {
		super(tunnelClient);
	}

	@Override
	protected void registerToServer(ChannelHandlerContext ctx) {
		//连接成功，设置全局对象
		TunnelChannelHandlerContextManager.setChannelHandlerContext(ctx);
		//注册
		String localHost = "...";
		try {
			localHost = InetAddress.getLocalHost().getHostAddress();
		} catch (Exception e) {
			LogUtil.error(e);
		}
		String content = Config.NAME + "[" + localHost + "]" + Constant.SPLIT_FLAG + 
						(StringUtil.isEmpty(Config.HOST_ARY)?Constant.NULL:Config.HOST_ARY) + Constant.SPLIT_FLAG +
						(StringUtil.isEmpty(Config.TCP_ARY)?Constant.NULL:Config.TCP_ARY) + Constant.SPLIT_FLAG +
						SysConfig.username() + Constant.SPLIT_FLAG +
						MD5Util.getMD5Code(SysConfig.password());
		TunnelDataQueueManager.commitData(TunnelDataQueueManager.REGISTER_MSG, ctx, content.getBytes());
	}
	
	@Override
	public void doRegisterResult(ChannelHandlerContext ctx, byte[] data) {
		super.doRegisterResult(ctx, data);
	}
	
	@Override
	public void httpDataMsg(ChannelHandlerContext ctx, byte[] data) {
		super.httpDataMsg(ctx, data);
	}
}